package com.wpw.java7demo.collections;

import java.util.*;
import java.util.Map.Entry;

import org.junit.Test;

public class CollectionTest {

	String[] array = {"q", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z"};

	@Test
	public void test1() {
		Map<IfCompareDemo, String> treeMap = new TreeMap<>(new Comparator<IfCompareDemo>() {

			@Override
			public int compare(IfCompareDemo o1, IfCompareDemo o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});
		//Map<IfCompareDemo, String> treeMap = new TreeMap<>();
		IfCompareDemo demo;
		for (int i = 0; i < array.length; i++) {
			demo = new IfCompareDemo();
			demo.setName(array[i]);
			//如果key不可比较，会抛出异常
			treeMap.put(demo, array[i]);
		}

		System.out.println(treeMap);
	}

	@Test
	public void test2() {
		Set<IfCompareDemo> treeSet = new TreeSet<>();
		IfCompareDemo demo;
		for (int i = 0; i < array.length; i++) {
			demo = new IfCompareDemo();
			demo.setName(array[i]);
			//如果元素不可比较，会抛出异常
			treeSet.add(demo);
		}

		System.out.println(treeSet);
	}

	@Test
	public void test3() {

		String[] array = {"q", "e", "r", "t", "e"};

		Set<IfCompareDemo> treeSet = new TreeSet<>(new Comparator<IfCompareDemo>() {

			@Override
			public int compare(IfCompareDemo o1, IfCompareDemo o2) {
				int compareTo = o1.getName().compareTo(o2.getName());
				if (compareTo == 0) {

				}
				return compareTo;
			}
		});
		//Map<IfCompareDemo, String> treeMap = new TreeMap<>();
		IfCompareDemo demo;
		for (int i = 0; i < array.length; i++) {
			demo = new IfCompareDemo();
			demo.setName(array[i]);
			//如果key不可比较，会抛出异常
			treeSet.add(demo);
		}

		System.out.println(treeSet);
	}

	@Test
	public void test4() {
		StudentTest4 s = new StudentTest4();
		List<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		list.add("c");
		s.setList(list);
		for (String str : s.getList()) {
			//这里只获取一次
		}

	}

	@Test
	public void test5() {
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < 11; i++) {
			list.add(i);
		}
		int line = 5;
		List<Integer> sub1 = list.subList(0, line);
		List<Integer> sub2 = list.subList(line, list.size());
		System.out.println(sub1);
		System.out.println(sub2);
	}

	@Test
	public void test6() {
		Set<String> set = new HashSet<>();
		set.add("1");
		set.add("2");
		set.add("3");
		set.add("4");
		ArrayList<String> list = new ArrayList<>(set);
		list.remove(0);
		System.out.println(list);
		System.out.println(set);
		list.remove(0);
		list.remove(0);
		list.remove(0);
		System.out.println(list);
	}

	@Test
	public void test7() {
		List<String> list = null;
		if (list == null) {
			list = new ArrayList<>();
		}
		list.add("1");
		System.out.println(list);
	}

	@Test
	public void test8() {
		List<StudentTest4> list = new ArrayList<>();
		for (int i = 0; i < 4; i++) {
			StudentTest4 s = new StudentTest4();
			s.setNum(i);
			list.add(s);
		}
		/*list.sort(new Comparator<StudentTest4>() {

			@Override
			public int compare(StudentTest4 o1, StudentTest4 o2) {
				return o2.getNum().compareTo(o1.getNum());
			}
		});*/
		System.out.println(list);
	}

	@Test
	public void test9() {
		Map<Integer, Integer> map = new HashMap<>();
		map.put(1, 1);
		map.put(2, 2);
		map.put(3, 3);
		/*for (int i = 0; i < map.size(); i++) {
			map.remove((i+1));
			System.out.println(map.size());
		}*/

		/*for (Integer key : map.keySet()) {
			map.remove(key);
		}*/
		/*for (Entry<Integer, Integer> entry : map.entrySet()) {
			Integer key = entry.getKey();
			map.remove(key);
		}*/
		Set<Entry<Integer, Integer>> entrySet = map.entrySet();
		Iterator<Entry<Integer, Integer>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Map.Entry<Integer, Integer> entry = iterator.next();
			iterator.remove();

		}

		System.out.println(map.size());

	}

	@Test
	public void test10() {
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			list.add(i);
		}
		List<Integer> subList = list.subList(0, 3);
		List<Integer> tempList = new ArrayList<>(subList);
		System.out.println(subList);
		System.out.println("移除前：" + list);
		boolean removeAll = list.removeAll(subList);
		System.out.println("移除后：" + list);
		System.out.println("移除后subList：" + tempList);
	}

	@Test
	public void qtyTest() {
		int needQty = 10;
		List<Integer> list = Arrays.asList(9, 4, 6, 6);
		List<Integer> intoList = new ArrayList<>();
		for (Integer allowQty : list) {
			Integer intoQty = needQty;
			if (needQty > allowQty) {
				intoQty = allowQty;
			}
			needQty = needQty - intoQty;

			intoList.add(intoQty);
			System.out.println("needQty  " + needQty);
			if (needQty <= 0) {
				break;
			}
		}
		System.out.println(intoList);

	}

}
class StudentTest4 {
	
	private List<String> list;
	
	private Integer num;

	public List<String> getList() {
		System.out.println("获取List");
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	@Override
	public String toString() {
		return "StudentTest4 [list=" + list + ", num=" + num + "]";
	}

	
}
